/*
   @Copyright:LeetCode
   @Author:   tjyemail
   @Problem:  http://leetcode.com/problems/convert-sorted-array-to-binary-search-tree
   @Language: C++
   @Datetime: 19-07-16 17:44
   */

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
	TreeNode *preorder(vector<int> &nums, int begin, int back){
		if(begin>back) return NULL;
		int mid = (begin+back)/2;
		TreeNode *r = new TreeNode(nums[mid]);
		r->left = preorder(nums, begin, mid-1);
		r->right = preorder(nums, mid+1, back);
		return r;
	}
public:
	TreeNode* sortedArrayToBST(vector<int>& nums) {
		return preorder(nums, 0, nums.size()-1);
	}
};
